package com.mrd.frame.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrd.frame.dao.AccessConfigDao;
import com.mrd.frame.dao.ConfigDao;
import com.mrd.frame.domain.AccessConfig;
import com.mrd.frame.domain.Config;
import com.mrd.frame.domain.message.CommonMessage;
import com.mrd.frame.domain.params.BaseDto;
import com.mrd.frame.remote.Validate;
import com.mrd.frame.service.AccessConfigService;

@Controller
public class AccessConfigController {
	
    private static final Log logger = LogFactory
			.getLog(AccessConfigController.class);
    
	@Autowired
	private Validate validate;
    @Autowired
 	private AccessConfigService accessConfigService;
	
    @RequestMapping(value = "/accessConfig", method = RequestMethod.GET)
	public String accessConfig(){
		return "/config/AccessConfig";
	}
    
    @ResponseBody
	@RequestMapping("/accessConfigSaveOrUpdate")
	public CommonMessage saveOrUpdate(@ModelAttribute AccessConfig accessConfig) {
		CommonMessage msg = new CommonMessage();
		String validateResult = validate.checkParams(accessConfig);
		if(validateResult != null){
			msg.setMessage(validateResult);
			return msg;
		}
		try {
			if (StringUtils.isNotEmpty(accessConfig.getAccessConfigId())) {// 更新操作
				accessConfigService.updateByPrimaryKeySelective(accessConfig);
			} else {// 新增
				int i = accessConfigService.insertSelective(accessConfig);
				if(i >= 0){
					msg.setMessage("保存成功");
				}else if(i == -3){
					msg.setCode(-1);
					msg.setMessage("支付渠道已存在");
				}
			}
		} catch (Exception e) {
			msg.setCode(-1);
			msg.setMessage(e.getMessage());
			logger.error(e);
			e.printStackTrace();
		}
		return msg;
	}
    
    @ResponseBody
    @RequestMapping("/accessConfigDelete")
    public CommonMessage accessConfigDelete(String ids) {
    	CommonMessage msg = new CommonMessage();
    	try {
    		accessConfigService.deleteByPrimaryKeys(ids);
    		msg.setCode(0);
    		msg.setMessage("删除成功");
    	} catch (Exception e) {
    		msg.setCode(-1);
    		msg.setMessage(e.getMessage());
    		logger.error(e);
    		e.printStackTrace();
    	}
    	return msg;
    }
    
    @ResponseBody
    @RequestMapping("/accessConfigGetById")
    public BaseDto accessConfigGetById(String id) {
    	AccessConfig  accessConfig = accessConfigService.selectByPrimaryKey(id);
    	BaseDto dto = new BaseDto();
    	dto.setObjDetail(accessConfig);
    	return dto;
    }
    
    @ResponseBody
    @RequestMapping("/accessConfigGetPageList")
    public BaseDto accessConfigGetPageList(@ModelAttribute AccessConfig accessConfig, @ModelAttribute BaseDto bdt) {
    	if(StringUtils.isEmpty(accessConfig.getUserKey())){
    		accessConfig.setUserKey(null);
    	}
    	if(StringUtils.isEmpty(accessConfig.getPayChannel())){
    		accessConfig.setPayChannel(null);
    	}
    	PageHelper.startPage(bdt.page,bdt.pageSize);
    	List<AccessConfig> accessConfigList = accessConfigService.selectSelective(accessConfig);
    	PageInfo<AccessConfig> pi = new PageInfo<AccessConfig>(accessConfigList);
    	bdt.setTotal((int) pi.getTotal());
    	bdt.setRows(accessConfigList);
    	return bdt;
    }
    
    /**
     * 初始化配置项到缓存
     * @author han
     */
    @ResponseBody
    @RequestMapping("/initAccessConfig")
    public CommonMessage initAccessConfig(){
    	CommonMessage msg = new CommonMessage();
    	try {
    		accessConfigService.initAccessConfig();
    		msg.setCode(0);
    		msg.setMessage("初始化缓存成功");
    	} catch (Exception e) {
    		msg.setCode(-1);
    		msg.setMessage(e.getMessage());
    	}
    	return msg;
    }
    
}